<?php

class ReportsController extends ReportsAppController
{
 	var $name = 'Reports';
	var $uses = array('Order');
	var $helpers = array('Price');	

/**
 * show basic report
 */

	function beforeFilter() {
        parent::beforeFilter();
		$this->today = date("Y.m.d");
		$this->DateTime = explode(".", $this->today);
		$this->year = $this->DateTime[0];
		$this->month = $this->DateTime[1];
		$this->day = $this->DateTime[2];
		Configure::write('debug', 2);
	}

/**
 * show basic report
 */

	function admin_index() {
		$freq = 1;
		if(isset($this->data['Frequence'])){
			
		}
		$titles = array (1 => 'Week', 2 => 'Month', 3 => 'Quarter', 4 => 'Year');		
		$choices = array (1 => 'Weekly', 2 => 'Monthly', 3 => 'Quarterly', 4 => 'Yearly');
		if(isset($this->data['Report'])) {
			$freq = $this->data['Report']['choices'];			
		}
		{
			$data = $this->_preset($freq);
		}
		$data[0]['frequence'] = $titles [$freq];
		//debug($data);
		if (isset($this->params['requested'])) {
			return $data;
		}
		$this->set(compact('data'));
		$this->set(compact('choices'));
	}

/*
 *
 */

	function _preset($freq) {
		return $this->__makeData($this->__getOrdersByWeek($freq), $freq);
		return $data;
	}

/**
 * show yearly
 */

	function __getOrders($date) {
		return $this->Order->find('all', array('conditions' => array('Order.created > ' => $date)));
	}
	function __getOrdersByWeek($freq) {		
		if ($freq == 1) //weekly
			return $this->Order->find('all'); //
		if ($freq == 2) //monthly
			return $this->Order->find('all'); //
		if ($freq == 3) //quarterly
			return $this->Order->find('all'); //
		if ($freq == 4) //yearly
			return $this->Order->find('all'); //
		
	}

/**
 * show basic report
 */

	function __custom($data) {
		$start = $data['start']['year'] . '-' . $data['start']['month'] . '-' . $data['start']['day'] . ' 00:00:00';
		$end = $data['end']['year'] . '-' . $data['end']['month'] . '-' . $data['end']['day'] . ' 00:00:00';
		return $this->Order->find('all', array('conditions' => array('Order.created' => 'BETWEEN ' . $start . ' AND ' . $end)));
	}


/**
 * count totals
 */
	function __makeData($data, $freq){
		if (!isset($data[0][0])) return null;
		$rows = array();
		$max = 10;
		$count = 0;
		$count2 = 0;
		$start = $data[0][0]["frequence"];
		$year = $data[0][0]["year"];
		for ($count=0; $count < $max; $count++){
			if ($start == $data[$count2][0]["frequence"]){
				$rows[$count] = $data[$count2][0];				
				$count2 ++;
			}else{
				$rows[$count] = array('quantity' => 0, 'total' => 0, 'frequence' => $start, 'year' => $year);
			}
			if ($freq == 1) $rows[$count]['date'] = $this->weekStartDate($start, $year);
			else $rows[$count]['date'] = $start;
            $rows[$count]['date']=(string)$rows[$count]['date'];
			$rows[$count]['quantity']=intval($rows[$count]['quantity']);
			$rows[$count]['total']=intval($rows[$count]['total']);
			$start --;
			if ($start <= 0) break;
		}
		return $rows;
	}

	function weekStartDate($weekNum, $year) {
		$date = mktime(0, 0, 0, 1, 1 + $weekNum*7-date('N',mktime(0,0,0,1,1,$year)), $year);
		return date('d-m-Y', $date);
	}

/**
 *
 */	
    function admin_info() {
		$this->redirect('/admin/reports/reports/');
    }


}
?>